<?php
include_once("UserIncludes/DbConfig.php");
if($_SESSION['langId']!="")
	$langId=$_SESSION['langId'];
else
	$langId=1;
function IP2Country($ipAddr)
{

$location = $ipAddr;
$q = 'http://api.local.yahoo.com/MapsService/V1/geocode?appid=rlerdorf&location='.rawurlencode($location);
$resp=file_get_contents($q);
$xml = simplexml_load_string($resp);
$countryShortName = $xml->Result[0]->Country;
$getCountryName = mysql_fetch_array(mysql_query("select countryName from country where countries_iso_code_2='".$countryShortName."'"));
return $getCountryName[0];
}
?>
<script language="JavaScript">

	function countt(mode,url,id)
	{
		document.form1.url.value=url;
		document.form1.id.value=id;
		document.form1.submit();
	}

</script>
<?php


if($_POST['getCountryName']=='yes')
{
post();
}

	function post()
	{
	$IP=$_SERVER['REMOTE_ADDR'];
	$counName=IP2Country($IP);
	//===================================================================
	
	$spaceid=$_REQUEST['id'];
	$counter=1;
	$chkcountry="SELECT * FROM ".ADTARGETTOPCOUNTRY." WHERE adCountryName='".$counName."' AND adspaceId='".$spaceid."'";
	$chkcountryQUERY=mysql_query($chkcountry);
	$rows=mysql_num_rows($chkcountryQUERY);


	if($rows>0)
	{
	$countersql="SELECT noOfClicks FROM ".ADTARGETTOPCOUNTRY." WHERE adCountryName='".$counName."' AND adspaceId='".$spaceid."'";
	$counterQuery=mysql_query($countersql);
	$fetchcountry=mysql_fetch_array($counterQuery);
	$upcounter=$fetchcountry['noOfClicks']+1;
		$UPDATE="UPDATE ".ADTARGETTOPCOUNTRY." SET adCountryName='".$counName."',noOfClicks='".$upcounter."' WHERE adspaceId='".$spaceid."'";
		$updatequery=mysql_query($UPDATE);
//  echo $UPDATE;
// 	die();


	}
	else
	{
	 $insert="INSERT INTO ".ADTARGETTOPCOUNTRY." SET adspaceId='".$spaceid."',adCountryName='".$counName."',noOfClicks='".$counter."',langId='".$_REQUEST[langId]."'";
	$insertQuery=mysql_query($insert);
// echo '<script> window.location="adSpaceDetails.php?id='.$_REQUEST['id'].'"</script>';

	}

	mysql_query("update ".ADSPSCEMASTER." set totClicksPerDay=totClicksPerDay+1 where adspaceId='".$spaceid."'");
	$getHighestDuration = mysql_fetch_array(mysql_query("select max(duration) from ".PRICE." where adspaceId='".$spaceid."'"));
	
	$getTotalClicks = mysql_fetch_array(mysql_query("select totClicksPerDay from ".ADSPSCEMASTER." where adspaceId='".$spaceid."'"));
	if($getHighestDuration[0]>0)
		$avgClickPerDay = ceil($getTotalClicks[0]/$getHighestDuration[0]);
	else
		$avgClickPerDay = $getTotalClicks[0];
		mysql_query("update ".ADSPSCEMASTER." set clickPerDay='".$avgClickPerDay."' where adspaceId='".$spaceid."'");
	
	$getUrl = mysql_fetch_array(mysql_query("select ".SITEMASTER.".adSiteUrl from ".SITEMASTER.",".ADSPSCEMASTER." where    	  	".SITEMASTER.".adsiteId=".ADSPSCEMASTER.".adsiteId   and ".ADSPSCEMASTER.".adspaceId='".$spaceid."'"));

 	echo '<script> window.location="'.$getUrl[0].'"</script>';

}



?>


<form name="form1" method="POST" action="">
<input type="hidden" name="getCountryName" value="yes">
<input type="hidden" name="url" value="">
<input type="hidden" name="id" value="">
<input type="hidden" name="langId" value="<?php echo $langId; ?>">

</form>
